////////////////////////////////////////////////////////////////////////////
//
//  CryEngine Source File.
//  Copyright (C), Crytek, 1999-2011.
// -------------------------------------------------------------------------
//  File name:   FlowGFxNodes.cpp
//  Version:     v1.00
//  Created:     10/11/2011 by Paul Reindell
//  Description: 
// -------------------------------------------------------------------------
//  History:
////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "FlowBaseNode.h"
#include <IFlashUI.h>

class CFlowNode_LoadTexIntoMcNode : public CFlowBaseNode<eNCT_Singleton>
{
public:
	CFlowNode_LoadTexIntoMcNode( SActivationInfo * pActInfo ) {};

	virtual void GetConfiguration( SFlowNodeConfig &config )
	{
		static const SInputPortConfig in_config[] = {
			InputPortConfig_Void( "Set",_HELP("Loads Texture into MovieClip") ),
			InputPortConfig<int>( "InstanceID",_HELP("") ),
			InputPortConfig<string>( "Texture",_HELP("") ),
			InputPortConfig<string>( "uiMovieclips_MovieClip",_HELP("") ),
			InputPortConfig_Null()
		};
		static const SOutputPortConfig out_config[] = {
			OutputPortConfig_Null()
		};
		config.sDescription = _HELP("");
		config.pInputPorts = in_config;
		config.pOutputPorts = out_config;
		config.SetCategory(EFLN_APPROVED);
	}
	virtual void ProcessEvent( EFlowEvent evt, SActivationInfo *pActInfo )
	{
		switch (evt)
		{
		case eFE_Initialize:
			break;
		case eFE_Activate:
			if (IsPortActive(pActInfo,eI_Set))
			{
				IUIElement* pUIElement = NULL;
				string sMovieClip;
				ITexture* pTexture;

				if (GetElementMc(pActInfo, pUIElement, sMovieClip, pTexture))
				{
					pUIElement->LoadTexIntoMc(sMovieClip.c_str(), pTexture);
				}
			}
			break;
		}
	};

	virtual void GetMemoryUsage(ICrySizer * s) const
	{
		s->Add(*this);
	}

private:
	enum EInputs
	{
		eI_Set = 0,
		eI_InstanceID,
		eI_Texture,
		eI_Mc,
	};


	bool GetElementMc(SActivationInfo *pActInfo, IUIElement*& pUIElement, string& sMovieClip, ITexture*& pTexture)
	{
		string sStr = GetPortString(pActInfo,eI_Mc);
		string::size_type sPos = sStr.find( ':' );

		if ( sPos != string::npos )
		{
			string sUIElement = sStr.substr( 0, sPos );
			sMovieClip = sStr.substr( sPos + 1 );

			int instanceId = GetPortInt(pActInfo, eI_InstanceID);
			string sTexture = GetPortString(pActInfo, eI_Texture);

			pTexture = gEnv->pRenderer->EF_LoadTexture(sTexture.c_str());

			IUIElement* pElement = gEnv->pFlashUI->GetUIElement(sUIElement.c_str());
			if (pTexture && pElement)
			{
				pUIElement = pElement->GetInstance(instanceId);
				return pUIElement->IsValid();
			}
		}
		return false;
	}
};

REGISTER_FLOW_NODE("GFx:LoadTexIntoMc", CFlowNode_LoadTexIntoMcNode);